Retrieving Data Using a DataReader

The DataReader provides the fastest but least flexible way to retrieve data from the database. Data is returned as a read-only, forward-only stream of data that is returned one record at a time. If you need to retrieve many records rapidly, using a DataReader would require less memory than a DataSet, which would need to create a large table to hold the results.

The following code example shows how to execute a simple query on a Sybase database and read the results using a DataReader.

NOTE: The example requires the emp table (see "Sample Tables for Sybase"). The Sybase database in this example does not require the Database connection string option.

// Open connection to SequeLink Server on Sybase database 
SequeLinkConnection  Conn; 
Conn = new SequeLinkConnection("host=bowhead;port=19996;User ID=test01; 
                                Password=test01"); 
Conn.Open(); 
// Create a SQL command 
string strSQL = "SELECT ename FROM emp WHERE sal>50000"; 
SequeLinkCommand   myCommand = new SequeLinkCommand(strSQL, Conn); 
SequeLinkDataReader  myDataReader; 
myDataReader = myCommand.ExecuteReader() 
try 
{ 
   while (myDataReader.Read()) 
   { 
    Console.WriteLine("High salaries:" + my DataReader["ename"].ToString()); 
   } 
} 
catch (Exception ex)  
{ 
    // Display any exceptions in a messagebox 
    MessageBox.Show (ex.Message); 
} 
//  Always close the DataReader  
myDataReader.Close(); 
//  Close the connection  
Conn.Close();